//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using FormLabel = CommunityServer.ControlPanel.Controls.FormLabel;
using HelpIcon = CommunityServer.ControlPanel.Controls.HelpIcon;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;

namespace CommunityServer.ControlPanel
{
	/// <summary>
	/// Summary description for _Default.
	/// </summary>
	public class SettingsPage : Page
	{
		protected MPContent DescriptionRegion;
		protected MPContent TaskRegion;
		protected ResourceLinkButton Save;
		protected YesNoRadioButtonList ynExpandGrouping;
		protected YesNoRadioButtonList ynEnableGrouping;
		protected YesNoRadioButtonList ynEnableSearchBox;
		protected YesNoRadioButtonList ynScalePageSize;
		protected YesNoRadioButtonList ynShowAdvanced;
		protected DropDownList GridMode;
		protected MPContent PanelDescription;
		protected HelpIcon Helpicon4;
		protected FormLabel Formlabel4;
		protected HelpIcon Helpicon5;
		protected FormLabel Formlabel5;
		protected HelpIcon Helpicon3;
		protected FormLabel Formlabel3;
		protected HelpIcon Helpicon6;
		protected FormLabel Formlabel6;
		protected HelpIcon Helpicon7;
		protected FormLabel Formlabel7;
		protected HelpIcon Helpicon8;
		protected FormLabel Formlabel8;
		protected ResourceLinkButton SaveButton;
		protected MPContainer MPContainer;
		protected User u;
	
		private void Page_Load(object sender, EventArgs e)
		{
			u = CSContext.Current.User;
			if(!Page.IsPostBack)
			{
				if(u != null)
				{
					ynExpandGrouping.Items.FindByValue(Globals.SafeBool(u.GetExtendedAttribute("CPExpandGroups"),true).ToString()).Selected = true;	
					ynEnableGrouping.Items.FindByValue(Globals.SafeBool(u.GetExtendedAttribute("CPEnableGroups"),false).ToString()).Selected = true;	
					ynEnableSearchBox.Items.FindByValue(Globals.SafeBool(u.GetExtendedAttribute("CPEnableSearchBox"),false).ToString()).Selected = true;	
					ynShowAdvanced.Items.FindByValue(Globals.SafeBool(u.GetExtendedAttribute("CPShowAdvanced"),false).ToString()).Selected = true;	
	
					ListItem li;

					string gridmode = u.GetExtendedAttribute("CPGridMode");
					li = GridMode.Items.FindByValue(gridmode.ToString());
					if(li != null)
						li.Selected = true;

					//The scale page size allows page designers to set how many rows a normal grid row will take to render, and 
					//should allow for more consistant grid rendering sizes (This proprty has been disabled by default to prevent confusion)
					ynScalePageSize.Items.FindByValue(Globals.SafeBool(u.GetExtendedAttribute("CPPageSizeScale"),false).ToString()).Selected = true;	
				}
			}
		}

		public void Save_Click(object sender, EventArgs e)
		{
			if(!CSContext.Current.User.IsAdministrator)
				throw new CSException(CSExceptionType.AccessDenied);

			u.SetExtendedAttribute("CPExpandGroups", ynExpandGrouping.SelectedValue.ToString());
			u.SetExtendedAttribute("CPPageSizeScale", ynScalePageSize.SelectedValue.ToString());
			u.SetExtendedAttribute("CPEnableGroups", ynEnableGrouping.SelectedValue.ToString());
			u.SetExtendedAttribute("CPEnableSearchBox", ynEnableSearchBox.SelectedValue.ToString());
			u.SetExtendedAttribute("CPShowAdvanced", ynShowAdvanced.SelectedValue.ToString());
			u.SetExtendedAttribute("CPGridMode", GridMode.SelectedValue.ToString());

			Users.UpdateUser(u);
		}

		protected virtual void Authorize()
		{
			if(!CSContext.Current.IsAuthenticated)
				Response.Redirect(Globals.GetSiteUrls().Login);
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			Authorize();
			InitializeComponent();
			base.OnInit(e);
			this.Save.Click += new EventHandler(Save_Click);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new EventHandler(this.Page_Load);

		}


		#endregion
	}
}
